package org.example.demo1.controller;

import org.example.demo1.common.PageData;
import org.example.demo1.common.PageQuery;
import org.example.demo1.common.ResponseData;
import org.example.demo1.domain.UserInfo;
import org.example.demo1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping
    public ResponseData<UserInfo> addUser(@RequestBody UserInfo userInfo) {
        return userService.addUser(userInfo);
    }

    @GetMapping("/{name}")
    public ResponseData<UserInfo> getUserByName(@RequestParam String userName) {
        return userService.getUserByName(userName);
    }

    @GetMapping("/phone/{phone}")
    public ResponseData<UserInfo> getUserByPhone(@RequestParam String phone) {
        return userService.getUserByPhone(phone);
    }

    @PutMapping
    public ResponseData<UserInfo> updateUser(@RequestBody UserInfo userInfo) {
        return userService.updateUser(userInfo);
    }

    @DeleteMapping("/{id}")
    public ResponseData<UserInfo> deleteUserByName(@RequestParam String userName) {
        return userService.deleteUserByName(userName);
    }

    @GetMapping("/page")
    public ResponseData<PageData<UserInfo>> page(@RequestBody PageQuery pageQuery) {
        return userService.page(pageQuery);
    }
}
